package com.homework.boot_demo.controller;

import com.homework.boot_demo.dao.Student;
import com.homework.boot_demo.service.Response;
import com.homework.boot_demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;


    @GetMapping("student/{id}")
    public Response<Student> getStudentById(@PathVariable int id){
       return Response.newSuccess(studentService.getStudentById(id));
    }

    @PostMapping("/student")
    public Response<Integer> addNewStudent(@RequestBody Student student){
        return  Response.newSuccess(studentService.addNewStudent(student));
    }

    @DeleteMapping("/student/{id}")
    public void deleteStudentById(@PathVariable int id){
        studentService.deleteStudentById(id);
    }

    @PutMapping("/student/{id}")
    public Response<Student> updateStudentById(@PathVariable int id, @RequestParam(required = false)String name,
                                               @RequestParam(required = false)String email) {
        return Response.newSuccess(studentService.updateStudentById(id,name,email));
    }
}
